import Vue from 'vue'
import './config'

//cors
// var cors=require('cors');
// app.use(cors({
//     origin:['http://localhost:9002'],
//     methods:['GET','POST'],
//     alloweHeaders:['Content-Type','Authorization']
// }));

const axios = Vue.prototype.$http
const $http = {
  get (url, params = {}, errors = []) {
    return new Promise((resolve, reject) => {
      axios.get(url, {params}).finally(() => {
        // setTimeout(() => { Vue.$vux.loading.hide() }, 500)
      }).then(res => {
        console.log(res)
        let { code, msg } = res.data
        // if (Number(code) !== 1) {
        //   let error = errors.find(error => {
        //     return Number(code) === error.code
        //   })
        //   console.log(error)
        //   msg = error ? error.msg : msg
        //   if (Number(code) !== 4) {
        //     setTimeout(() => {
        //       Vue.$vux.alert.show({
        //         title: '提示',
        //         content: msg,
        //         hideOnBlur: true
        //       })
        //     }, 500)
        //   }
        //   reject(res.data)
        // } else {
          resolve(res.data)
        // }
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },
  post(url, body, params = {}, errors = []) {
    return new Promise((resolve, reject) => {
      axios.post(url, {...body, session_id: sessionStorage.getItem('session')}, {params}).finally(() => {
        setTimeout(() => {
          Vue.$vux.loading.hide();
        }, 500);
      }).then(res => {
        let {code, msg} = res.data;
        if (Number(code) !== 200) {
          reject(res);
        } else {
          resolve(res.data);
        }
      }).catch(error => {
        console.log(error);
        reject(error);
      });
    });
  },
  put (url, body, params = {}, errors = []) {
    return new Promise((resolve, reject) => {
      axios.put(url, body, {params}).finally(() => {
        setTimeout(() => { Vue.$vux.loading.hide() }, 500)
      }).then(res => {
        let { code, msg } = res.data
        if (Number(code) !== 1) {
          let error = errors.find(error => {
            return Number(code) === error.code
          })
          msg = error ? error.msg : msg
          setTimeout(() => {
            Vue.$vux.alert.show({
              title: '提示',
              content: msg,
              hideOnBlur: true
            })
          }, 500)
          reject(res)
        } else {
          resolve(res.data)
        }
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  },
  delete (url, errors = []) {
    return new Promise((resolve, reject) => {
      axios.delete(url).finally(() => {
        setTimeout(() => { Vue.$vux.loading.hide() }, 500)
      }).then(res => {
        let { code, msg } = res.data
        if (Number(code) !== 1) {
          let error = errors.find(error => {
            return Number(code) === error.code
          })
          msg = error ? error.msg : msg
          setTimeout(() => {
            Vue.$vux.alert.show({
              title: '提示',
              content: msg,
              hideOnBlur: true
            })
          }, 500)
          reject(res)
        } else {
          resolve(res.data)
        }
      }).catch(error => {
        console.log(error)
        reject(error)
      })
    })
  }
}

/*
 用户
*/

// 登陆
export const login = params => {
  return $http.post(`/customer/login`,params)
}
// 注册
export const register = params => {
  return $http.post(`/customer/create`,params)
}

// 获取微信JS权限
export const getWx = params => {
  return axios.get(`/wx/jssdk`, {params}).then(res => res.data)
}
// 获取token
export const getToken = body => {
  return $http.post(`/token/pc`, body)
}

// 上传图片
export const uploadImg = body => {
  return $http.post(`/upload/image`, body)
}

// 获取用户信息
export const getUser = body => {
  return $http.post(`/customer/detail`, body)
}

// 修改用户信息
export const editUser = body => {
  return $http.post(`/customer/edit`, body)
}
// 关注用户
export const followUser = params => {
  return $http.post(`/customer_follow/create`, params)
}

// 获取文章列表
export const getArticle = params => {
  return $http.post(`/article/finds`, params)
}

// 创建文章
export const createArticle = params => {
  return $http.post(`/article/create`, params)
}

// 文章详情
export const getArticledatail = params => {
  return $http.post(`/article/detail`, params)
}

// 获取留言
export const getMessage = params => {
  return $http.post(`/message/finds`, params)
}

// 发表留言
export const submitMessage = params => {
  return $http.post(`/message/create`, params)
}

// 获取文章评论
export const getComment = params => {
  return $http.post(`/message/findsComment`, params)
}


// 点赞
export const submitLike = params => {
  return $http.post(`/message_like/create`, params)
}

// 获取资讯列表
export const getNews = params => {
  return $http.post(`/news_article/finds`, params)
}

// 获取资讯分类
export const getNewscate = params => {
  return $http.get(`/News/get_newscate`, params)
}

// 获取资讯详情
export const getNewsdatail = params => {
  return $http.post(`/news_article/detail`, params)
}

// 发表评论
export const submitComment = params => {
  return $http.get(`/Comment/create_comment`, params)
}

// 回复评论
export const submitReply = params => {
  return $http.post(`/reply/create`, params)
}

// 获取分类列表
export const getCate = params => {
  return $http.get(`/category/getCategory`, params)
}

// 获取轮播图列表
export const getBanner = params => {
  return $http.post(`/banner/finds`, params)
}


